package com.ruoyi.quality.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.quality.constant.MaterialPurchasingMode;
import com.ruoyi.quality.domain.QualityCustom;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quality.domain.QualityMaterial;
import com.ruoyi.quality.service.IQualityMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物料管理Controller
 *
 * @author wky
 * @date 2024-06-05
 */
@RestController
@RequestMapping("/quality/material")
@Tag(name = "【物料管理】管理")
public class QualityMaterialController extends BaseController
{
    @Autowired
    private IQualityMaterialService qualityMaterialService;

    /**
     * 查询物料管理列表
     */
    @Operation(summary = "查询物料管理列表")
    @PreAuthorize("@ss.hasPermi('quality:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(QualityMaterial qualityMaterial)
    {
        startPage();
        List<QualityMaterial> list = qualityMaterialService.selectQualityMaterialList(qualityMaterial);
        return getDataTable(list);
    }
    @Operation(summary = "查询客户管理列表")
    @PreAuthorize("@ss.hasPermi('quality:custom:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll()
    {
        List<QualityMaterial> list = qualityMaterialService.selectQualityMaterialList(null);
        return AjaxResult.success(list);
    }
    /**
     * 导出物料管理列表
     */
    @Operation(summary = "导出物料管理列表")
    @PreAuthorize("@ss.hasPermi('quality:material:export')")
    @Log(title = "物料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QualityMaterial qualityMaterial)
    {
        List<QualityMaterial> list = qualityMaterialService.selectQualityMaterialList(qualityMaterial);
        ExcelUtil<QualityMaterial> util = new ExcelUtil<QualityMaterial>(QualityMaterial.class);
        util.exportExcel(response, list, "物料管理数据");
    }

    /**
     * 获取物料管理详细信息
     */
    @Operation(summary = "获取物料管理详细信息")
    @PreAuthorize("@ss.hasPermi('quality:material:query')")
    @GetMapping(value = "/{materialId}")
    public AjaxResult getInfo(@PathVariable("materialId") Long materialId)
    {
        return success(qualityMaterialService.selectQualityMaterialByMaterialId(materialId));
    }

    /**
     * 新增物料管理
     */
    @Operation(summary = "新增物料管理")
    @PreAuthorize("@ss.hasPermi('quality:material:add')")
    @Log(title = "物料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QualityMaterial qualityMaterial)
    {
        return toAjax(qualityMaterialService.insertQualityMaterial(qualityMaterial));
    }

    /**
     * 修改物料管理
     */
    @Operation(summary = "修改物料管理")
    @PreAuthorize("@ss.hasPermi('quality:material:edit')")
    @Log(title = "物料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QualityMaterial qualityMaterial)
    {
        return toAjax(qualityMaterialService.updateQualityMaterial(qualityMaterial));
    }

    /**
     * 删除物料管理
     */
    @Operation(summary = "删除物料管理")
    @PreAuthorize("@ss.hasPermi('quality:material:remove')")
    @Log(title = "物料管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materialIds}")
    public AjaxResult remove(@PathVariable( name = "materialIds" ) Long[] materialIds)
    {
        return toAjax(qualityMaterialService.deleteQualityMaterialByMaterialIds(materialIds));
    }

    /*@Operation(summary = "导入物料")
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('quality:material:add')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<QualityMaterialVO> util = new ExcelUtil<>(QualityMaterialVO.class);
        List<QualityMaterialVO> materialVOList = util.importExcel(file.getInputStream());
        List<QualityMaterial> qualityMaterials = materialVOList.stream().map(item -> {
            QualityMaterial qualityMaterial = new QualityMaterial();
            if (item.getPurchasingModeName().equals(MaterialPurchasingMode.ORDER_GET.value)) {
                qualityMaterial.setPurchasingMode(MaterialPurchasingMode.ORDER_GET.key);
            }
            return qualityMaterial;
        }).toList();

        return success();
    }*/

}
